var prefix = jsctx + "/elecPile";

var pile = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPagePile",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            elecStationId: $("#elecStationId").val().trim(),
                            //  operateStatus: $("#operateStatus").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '所属桩群', width:'100',align: 'left'},
                        {field: 'power', title: '额定功率', width:'100',align: 'left'},
                        {
                            field: 'hasParking', title: '是否有车位', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>是</span>'
                                } else if (value == '1') {
                                    return '<span>否</span>'
                                }
                            }
                        },
                        {
                            field: 'hasLock', title: '是否有地锁', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>否</span>'
                                } else if (value == '1') {
                                    return '<span>是</span>'
                                }
                            }
                        },
                        {field: 'pileNo', title: '桩编号', width:'100',align: 'left'},
                        {field: 'pileName', title: '桩名称', width:'100',align: 'left'},
                        {
                            field: 'pileElecType', title: '电桩类型', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span>单直流</span>'
                                } else if (value == '2') {
                                    return '<span>单交流</span>'
                                } else if (value == '3') {
                                    return '<span>双直流</span>'
                                } else if (value == '4') {
                                    return '<span>双交流</span>'
                                }
                            }
                        },
                        {field: 'pileBand', title: '桩品牌', width:'100',align: 'left'},
                        {
                            field: '', title: '电枪型号', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                var z = '';

                                for (var i = 0; i < row.elecGunList.length; i++) {

                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);

                                    if (lastNum == '1') {
                                        var a = lastNum + '.';
                                        var b = "A型号电枪";

                                        var q = '<a class="btn btn-primary btn-xs ' + has_edit + '" href="#" title="二维码"   data-target="#myModal" data-toggle="modal"  onclick="pile.printElecGunQrCode(\''
                                            + row.elecGunList[i].gunNo
                                            + '\')">二维码</a> ';

                                        z += a + ' ' + b + ' ' + q + '<br>';
                                    } else if (lastNum == '2') {
                                        var a = lastNum + '.';
                                        var b = "B型号电枪";

                                        var q = '<a class="btn btn-primary btn-xs ' + has_edit + '" href="#" title="二维码"   data-target="#myModal" data-toggle="modal"  onclick="pile.printElecGunQrCode(\''
                                            + row.elecGunList[i].gunNo
                                            + '\')">二维码</a> ';

                                        z += a + ' ' + b + '' + q;
                                    }
                                }
                                return z;
                            }
                        },
                        {
                            field: '', title: '使用状态', width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                if (row.status == 0) {
                                    return '<a class="btn label-danger btn-xs " href="#">离线</a>'
                                } else {
                                    return z;
                                }
                            }
                        },
                        {
                            field: '', title: '电枪状态', width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">故障</a> ';
                                        }else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">故障</a> ';
                                        }else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                return z;
                            }
                        },
                        {
                            field: 'operateStatus', title: '桩运营状态',
                            width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">未上线</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">上线</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-danger">下线</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {

                                var d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="桩列表" onclick="pile.detailElecPile(\''
                                    + row.elecPileId
                                    + '\')">详情</a> ';

                                return d;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    function detailElecPile(elecPileId) {
        location.href = prefix + "/detailElecPile/" + elecPileId;
    }

    function goBack() {
        window.history.back();
    }


    function printElecGunQrCode(gunNo) {
        // location.href = prefix + "/genElecGunQrCode/" + gunNo;
        layer.open({
            type: 2,
            title: '枪号：' + gunNo,
            shadeClose: false,
            area: ['350px', '300px'],
            content: prefix + '/genElecGunQrCode/' + gunNo
        });
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        detailElecPile: detailElecPile,
        printElecGunQrCode: printElecGunQrCode
    };
})();
